const {
    Mysql
} = require('../tools/utils');
module.exports = async (ctx) => {
    try {
        let {
            id
        } = ctx.query;
        id = Buffer.from(id, 'base64').toString('utf-8');
        let sql = `SELECT * from cSessionInfo WHERE open_id='${id}'`;
        let userInfo = (await Mysql(sql))[0]
        let {
            uid
        } = userInfo;
        if (Number(uid) != 0) {
            /* let info = JSON.parse(userInfo.user_info);
            info.uid = uid;
            let infoStr = JSON.stringify(info); */
            ctx.state.data = {
                uid: Number(uid),
                code: '001'
            }
        } else {
            let uuid = Math.random().toString().slice(4, 10);
            if (uuid[0] == 0) {
                start = Math.floor(Math.random() * (9 - 1)) + 1;
                uuid = `${start}${uuid.substr(1,6)}`;
            };
            let sql = `SELECT * from cSessionInfo WHERE uid='${uuid}'`
            let haveUserId = await Mysql(sql);
            if (!haveUserId[0]) {
                let sql = `UPDATE cSessionInfo SET uid = ${uuid} WHERE open_id='${id}'`;
                await Mysql(sql);
                ctx.state.data = {
                    uid: Number(uuid),
                    code: '001'
                }
            }
        }
    } catch (error) {
        ctx.state.data = {
            code: '002'
        }
    }

}